using System;
using System.Data.Services;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using ALinq.Mapping;
using System.Collections;

namespace ALinq.BBS.DataAccess
{
    partial class Board
    {

    }

    partial class Database
    {
        private const int PostIdentity = 1;
        private const int ReplyIdentity = 2;
        private const int BoardIdentity = 3;
        private const int UserIdentity = 4;

        internal enum ObjectType
        {
            Post = 1,
            Reply = 2,
            Board = 3,
            User = 4,
        }

        partial void OnCreated()
        {
            var listenerName = Properties.Settings.Default.ListenerName;
            if (string.IsNullOrEmpty(listenerName) == false)
            {
                var listener = Trace.Listeners[listenerName] as TextWriterTraceListener;
                if (listener != null)
                    Log = listener.Writer;
            }
        }

        static XmlMappingSource CreateMappingSource()
        {
            var xmlMapping = XmlMappingSource.FromStream(typeof(Database).Assembly.GetManifestResourceStream("ALinq.BBS.Business.Database.designer.map"));
            return xmlMapping;
        }



        private const int INTERVAL = 10000;
        private const int MIN_PUBLIC_BULLETIN_ORDER = Int32.MaxValue - INTERVAL;
        private const int MIN_PUBLIC_TOP_ORDER = MIN_PUBLIC_BULLETIN_ORDER - INTERVAL;
        private const int MIN_BOARD_BULLETIN_ORDER = MIN_PUBLIC_TOP_ORDER - INTERVAL;
        private const int MIN_BOARD_TOP_ORDER = MIN_BOARD_BULLETIN_ORDER - INTERVAL;
        private const int MIN_BULLETIN_ORDER = MIN_BOARD_TOP_ORDER - INTERVAL;

        private const int MIN_TOP_ORDER = MIN_BULLETIN_ORDER - INTERVAL;
        private const int MAX_TOP_ORDER = MIN_BULLETIN_ORDER - 1;

        private const int MIN_NORMAL_ORDER = 0;
        private const int MAX_NORMAL_ORDER = MIN_TOP_ORDER - 1;

        private const int MIN_BOTTOM_ORDER = Int32.MinValue;
        private const int MAX_BOTTOM_ORDER = MIN_NORMAL_ORDER - 1;

        public int NewPostID()
        {
            var item = ObjectIdentities.Where(o => o.ID == ObjectType.Post).Single();
            item.MaxID++;
            //SubmitChanges();
            return item.MaxID;
        }

        public int NewReplyID()
        {
            var item = ObjectIdentities.Where(o => o.ID == ObjectType.Reply).Single();
            item.MaxID++;
            //SubmitChanges();
            return item.MaxID;
        }

        public int NewBoardID()
        {
            var item = ObjectIdentities.Where(o => o.ID == ObjectType.Board).Single();
            item.MaxID++;
            //SubmitChanges();
            return item.MaxID;
        }

        public int NewPostOrder(PostType type)
        {
            throw new NotImplementedException();
        }

        public int NewUserID()
        {
            var item = ObjectIdentities.Where(o => o.ID == ObjectType.User).Single();
            item.MaxID++;
            SubmitChanges();
            return item.MaxID;
        }

        public new void CreateDatabase()
        {
            base.CreateDatabase();
            var files = Enum.GetNames(typeof(ObjectType));
            foreach (var file in files)
            {
                var value = (ObjectType)Enum.Parse(typeof(ObjectType), file);
                ObjectIdentities.InsertOnSubmit(new ObjectIdentity { ID = value, MaxID = 1 });
            }

            var board = new Board { ID = Const.BoardRootID, Name = string.Empty };
            Boards.InsertOnSubmit(board);

            try
            {
                SubmitChanges();
            }
            catch (Exception)
            {
                DeleteDatabase();
                throw;
            }
        }


    }






}
